package com.ruoyi.bizsys.service.impl;

import java.util.List;
import java.util.Arrays;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.domain.BankAccount;
import com.ruoyi.bizsys.domain.CustBindCardLog;
import com.ruoyi.bizsys.domain.CustomerInfo;
import com.ruoyi.bizsys.service.ICustBindCardLogService;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.enums.RspEnmu;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bizsys.mapper.CustBankCardMapper;
import com.ruoyi.bizsys.domain.CustBankCard;
import com.ruoyi.bizsys.service.ICustBankCardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


/**
 * 客户绑定银行卡Service业务层处理
 *
 * @author african-loan
 * @date 2021-12-30
 */
@Service
public class CustBankCardServiceImpl extends ServiceImpl<CustBankCardMapper, CustBankCard> implements ICustBankCardService {

    @Autowired
    CustBankCardMapper custBankCardMapper;

    @Autowired
    ICustBindCardLogService custBindCardLogService;

    @Autowired
    ISysConfigService iSysConfigService;


    /**
     * 查询客户绑定银行卡
     *
     * @param id 客户绑定银行卡ID
     * @return 客户绑定银行卡
     */
    @Override
    public CustBankCard selectCustBankCardById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询客户绑定银行卡列表
     *
     * @param custBankCard 客户绑定银行卡
     * @return 客户绑定银行卡
     */
    @Override
    public List<CustBankCard> selectCustBankCardList(CustBankCard custBankCard) {
        return this.list(new QueryWrapper<>(custBankCard).last("order by id desc"));
    }


    /**
     * 后台使用
     * @param custBankCard 客户绑定银行卡
     * @return
     */
    @Override
    public List<CustBankCard> getList(CustBankCard custBankCard) {
        if(custBankCard.getParams().entrySet().size() > 0){
            return this.list(new QueryWrapper<>(custBankCard).ge("create_time",custBankCard.getParams().get("beginTime")).lt("create_time",custBankCard.getParams().get("endTime")).last("order by id desc"));
        }
        return this.list(new QueryWrapper<>(custBankCard).last("order by id desc"));
    }

    /**
     * 新增客户绑定银行卡
     *
     * @param custBankCard 客户绑定银行卡
     * @return 结果
     */
    @Override
    public int insertCustBankCard(CustBankCard custBankCard) {
        custBankCard.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(custBankCard);
    }

    /**
     * 修改客户绑定银行卡
     *
     * @param custBankCard 客户绑定银行卡
     * @return 结果
     */
    @Override
    public int updateCustBankCard(CustBankCard custBankCard) {
        custBankCard.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(custBankCard);
    }

    /**
     * 批量删除客户绑定银行卡
     *
     * @param ids 需要删除的客户绑定银行卡ID
     * @return 结果
     */
    @Override
    public int deleteCustBankCardByIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除客户绑定银行卡信息
     *
     * @param id 客户绑定银行卡ID
     * @return 结果
     */
    @Override
    public int deleteCustBankCardById(Long id) {
        return baseMapper.deleteById(id);
    }

    /**
     * 不要直接查询id要匹配是否当前用户操作
     *
     * @param custBankCard
     * @return
     */
    @Override
    public CustBankCard getDetailByCustIdCardId(CustBankCard custBankCard) {
        return baseMapper.getDetailByCustIdCardId(custBankCard);
    }

    @Override
    public String selectCustBankCardMaxCnt(String custNo) {
        return custBankCardMapper.selectCustBankCardMaxCnt(custNo);
    }

    @Override
    public int selectCustBankCardCnt(String custNo) {
        int i = custBankCardMapper.selectCustBankCardCnt(custNo);
        if (i > 0) {
            return custBankCardMapper.selectCustBankCardCnt(custNo);
        }
        return -1;
    }

    @Override
    public int customerCardAndTransCount(String custNo) {
        int count = customerBindCardCount(custNo);
        int transCount = custBindCardLogService.getTransCount(custNo);
        return count + transCount;
    }

    @Override
    public int getCardMaxLimit(CustomerInfo customerInfo) {
        //哪个比较小用哪个
        int needBindCardNum = customerInfo.getNeedBindCardNum();
        int bankListNum = Integer.parseInt(iSysConfigService.selectConfigByKey("bank_list_nums"));
        return needBindCardNum > bankListNum ? bankListNum : needBindCardNum;
    }

    private int customerBindCardCount(String custNo) {
        return count(new QueryWrapper<CustBankCard>().eq("cust_no", custNo).eq("state", UserConstants.BUSINESS_NORMAL));
    }

    @Override
    public int countByUserId(String userId) {
        QueryWrapper<CustBankCard> queryWrapper = new QueryWrapper<CustBankCard>().eq("user_id", userId);
        queryWrapper.eq("state",1);
        return count(queryWrapper);
    }

}
